class DirectoryModel {
  final int id;
  final String name;
  final int parentId;
  final int userId;
  final String path;
  final DateTime createTime;
  final DateTime updateTime;
  final bool isDeleted;
  final List<DirectoryModel>? children;
  final int? fileCount;

  DirectoryModel({
    required this.id,
    required this.name,
    required this.parentId,
    required this.userId,
    required this.path,
    required this.createTime,
    required this.updateTime,
    this.isDeleted = false,
    this.children,
    this.fileCount,
  });

  factory DirectoryModel.fromJson(Map<String, dynamic> json) {
    List<DirectoryModel> childrenList = [];
    if (json['children'] != null) {
      childrenList =
          (json['children'] as List)
              .map((child) => DirectoryModel.fromJson(child))
              .toList();
    }

    return DirectoryModel(
      id: json['id'],
      name: json['name'],
      parentId: json['parent_id'],
      userId: json['user_id'],
      path: json['path'],
      createTime: DateTime.parse(json['create_time']),
      updateTime: DateTime.parse(json['update_time']),
      isDeleted: json['is_deleted'] ?? false,
      children: json['children'] != null ? childrenList : null,
      fileCount: json['file_count'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'parent_id': parentId,
      'user_id': userId,
      'path': path,
      'create_time': createTime.toIso8601String(),
      'update_time': updateTime.toIso8601String(),
      'is_deleted': isDeleted,
    };
  }
}
